package com.dtx.facade.pagemaker;



import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

/**
 * PageMaker类使用Database类和HTMLWriter类来生成指定用户的Web页面。
 * 在该类中定义的方法只有一个,那就是public的makeWelcomePage方法。该方法会根据指定的邮件地址和文件名生成相应的Web页面。
 * PageMaker类一手包办了调用HtmlWriter类的方法这一工作。对外部,它只提供了makeWelcomePage接口。
 * 这就是一个简单窗口
 */
public class PageMaker {

    //防止外部new出PageMaker的实例,所以声明为private方法
    private PageMaker(){

    }

    public static void makeWelcomePage(String mailAddress,String fileName){
        String path = PageMaker.class.getClassLoader().getResource("facade/maildata.txt").getFile();
        try {
            //Properties mailProp = Database.getProperties("graphdesign"+ File.separator+"facade"+ File.separator+"maildata");
            String substring = path.substring(0, path.indexOf("."));
            Properties mailProp = Database.getProperties(substring);
            String username = mailProp.getProperty(mailAddress);
            HtmlWriter writer = new HtmlWriter(new FileWriter("graphdesign"+ File.separator+"facade"+ File.separator+fileName));
            writer.title("Welcome to " + username + "'s page!");
            writer.paraGraph(username + "欢迎来到" + username + "的主页。");
            writer.paraGraph("等着你的邮件哦! ");
            writer.mailto(mailAddress,username);
            writer.close();
            System.out.println(fileName + " is created for " + mailAddress + "(" + username + ")");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
